<?php
if (!class_exists('DoctrineTemplate')) {
	/**
	 *  Utility dao class for Doctrine ORM
	 *	@author Alecsandru Gabriel DINU
	 *  @since Jun 18, 2008
	 */
	class DoctrineTemplate {
		protected static $doctrine_config;

		/**
		 * Class constructor
		 * @access public
		 */
		public function __construct() {
			require_once('lib/doctrine/lib/Doctrine.php');
			spl_autoload_register(array('Doctrine', 'autoload'));
			
			$config = AlfaConfiguration::getInstance();
			$root = $config->getString("appRootDir") . "/";

			// Configure Doctrine Cli
			// Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
			$this->doctrine_config = array( 'data_fixtures_path'  =>  $root . $config->getString("database/doctrine/data-fixtures-path"),
					                		'models_path'         =>  $root . $config->getString("database/doctrine/models-path"),
					                		'migrations_path'     =>  $root . $config->getString("database/doctrine/migrations-path"),
					                		'sql_path'            =>  $root . $config->getString("database/doctrine/sql-path"),
					                		'yaml_schema_path'    =>  $root . $config->getString("database/doctrine/yaml-schema-path"),
					                		'dsn'                 =>  $config->getString("database/dsn"));
			
			define("STDOUT","php://stdout");
			
			Doctrine_Manager::connection($this->doctrine_config["dsn"]);
			Doctrine_Manager::getInstance()->setAttribute('model_loading', 'conservative');
		}
		
		/**
		 * Returns a Configuration instance for classes that 
		 * extend this template
		 * @access protected
		 * @return Configuration
		 */
		protected function getConfig() {
			return $this->doctrine_config;
		}

		/**
		 * Executes a Doctrine CLI command
		 * @access public
		 * @param string
		 */
		public function execute(string $command) {
			$cli = new Doctrine_Cli($this->doctrine_config);
			$args[0] = "doctrine";
			$args[1] = $command;
			$cli->run($args);			
		}
		
	}
}
?>